import bpy
import types

"""class MC_Vertex (bpy.types.MeshVertex) :
        def __init__ (self, bpys) :
            super (MC_Vertex, self).__init__(bpys)
            
class MC_Mesh (bpy.types.Mesh) :
        def __self__ (self, bpys) : 
            super (MC_Mesh, self).__init__(bpys)
            self.strange = 0
"""            
"""def funcToMethod(func,clas,method_name=None):
        func.im_class=clas
        func.im_func=func
        func.im_self=None
        if not method_name: method_name=func.__name__
        clas.__dict__[method_name]=func
"""

def set_color (_self, _color) :
    _self.color = _color

def get_color (_self) :
    return _self.color

def set_resolution (_self, _resolution) :
    _self.resolution = _resolution 

def get_resolution (_self) :
    return _self.resolution 

def insert_methods () :
    """Init the methods of type bpy.types.MeshVertex"""
    bpy.types.MeshVertex.set_color = types.MethodType(set_color, bpy.types.MeshVertex)
    bpy.types.MeshVertex.get_color = types.MethodType(get_color, bpy.types.MeshVertex)
    bpy.types.MeshVertex.set_resolution = types.MethodType(set_resolution, bpy.types.MeshVertex)
    bpy.types.MeshVertex.get_resolution = types.MethodType(get_resolution, bpy.types.MeshVertex)    

    """Init the methods of type bpy.types.MeshEdge"""
    bpy.types.MeshEdge.set_color = types.MethodType(set_color, bpy.types.MeshEdge)
    bpy.types.MeshEdge.get_color = types.MethodType(get_color, bpy.types.MeshEdge)
    bpy.types.MeshEdge.set_resolution = types.MethodType(set_resolution, bpy.types.MeshEdge)
    bpy.types.MeshEdge.get_resolution = types.MethodType(get_resolution, bpy.types.MeshEdge)    

    """Init the methods of type bpy.types.MeshFace"""
    bpy.types.MeshFace.set_color = types.MethodType(set_color, bpy.types.MeshFace)
    bpy.types.MeshFace.get_color = types.MethodType(get_color, bpy.types.MeshFace)
    bpy.types.MeshFace.set_resolution = types.MethodType(set_resolution, bpy.types.MeshFace)
    bpy.types.MeshFace.get_resolution = types.MethodType(get_resolution, bpy.types.MeshFace)    

    print ("Insert Methods Finish")

def init_meshcolor_data(context) :
    scene = context.scene
    obj_act = scene.objects.active

    # Can't edit anything, unless we have an active obj.
    if not obj_act:
        print ("No object is selected")
        return None

    # We got the active object. Now we set the default mesh_color value for these objects (A group of objects)
    # At first we only support one object. 
    
    # Set the color and resolution of vertices
    for v in obj_act.data.vertices :
        v.set_color ([0,0,0])

    for v in obj_act.data.vertices :
        print (v.get_color ())

insert_methods()
init_meshcolor_data(bpy.context)

"""
# Creat a new Mesh Sample
name = "Mesh"
mesh = bpy.data.meshes.new(name)
mesh.set_color(1)

print (mesh.get_color())
ob_new = bpy.data.objects.new(name, mesh)

# Link new object to the given scene and select it.
bpy.context.scene.objects.link(ob_new)
ob_new.select = True
"""
            
#bpy.ops.object.add(MC_Mesh)

def create_mesh_object(context, verts, edges, faces, name, edit, align_matrix):
    scene = context.scene
    obj_act = scene.objects.active

    # Can't edit anything, unless we have an active obj.
    if edit and not obj_act:
        return None

    # Create new mesh
    mesh = bpy.data.meshes.new(name)

    # Make a mesh from a list of verts/edges/faces.
    mesh.from_pydata(verts, edges, faces)

    # Update mesh geometry after adding stuff.
    mesh.update()

    # Deselect all objects.
    bpy.ops.object.select_all(action='DESELECT')

    if edit:
        # Replace geometry of existing object

        # Use the active obj and select it.
        ob_new = obj_act
        ob_new.select = True

        if obj_act.mode == 'OBJECT':
            # Get existing mesh datablock.
            old_mesh = ob_new.data

            # Set object data to nothing
            ob_new.data = None

            # Clear users of existing mesh datablock.
            old_mesh.user_clear()

            # Remove old mesh datablock if no users are left.
            if (old_mesh.users == 0):
                bpy.data.meshes.remove(old_mesh)

            # Assign new mesh datablock.
            ob_new.data = mesh

    else:
        # Create new object
        ob_new = bpy.data.objects.new(name, mesh)

        # Link new object to the given scene and select it.
        scene.objects.link(ob_new)
        ob_new.select = True

        # Place the object at the 3D cursor location.
        # apply viewRotaion
        ob_new.matrix_world = align_matrix

    if obj_act and obj_act.mode == 'EDIT':
        if not edit:
            # We are in EditMode, switch to ObjectMode.
            bpy.ops.object.mode_set(mode='OBJECT')

            # Select the active object as well.
            obj_act.select = True

            # Apply location of new object.
            scene.update()

            # Join new object into the active.
            bpy.ops.object.join()

            # Switching back to EditMode.
            bpy.ops.object.mode_set(mode='EDIT')

            ob_new = obj_act

    else:
        # We are in ObjectMode.
        # Make the new object the active one.
        scene.objects.active = ob_new

    return ob_new 
